package com.tunan.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.contant.Status;
import com.tunan.system.persistence.model.BscDicCodeItem;
import com.tunan.system.persistence.model.BscDicCodeType;

/**
 * 数据字典转换类
 * 
 * @author Administrator
 */
public final class BscDicCodeConverter {

	/**
	 * 私有构造方法，目的不允许外界使用够找方法
	 */
	private BscDicCodeConverter() {
	};

	/**
	 * 将数据字典项数据转换成下拉框json数据
	 * 
	 * @param list
	 *            数据字典项数据集合
	 * @param allowBlank
	 *            下拉框是否允许有空白项
	 * @return 下拉框json数据
	 */
	public static List<BasicCombo> convertCodeItemToBasicCombo(
			List<BscDicCodeItem> list, boolean allowBlank) {
		List<BasicCombo> result = new ArrayList<BasicCombo>();

		if (allowBlank) {
			result.add(BasicCombo.getBlankItem());
		}

		if (list != null && list.size() > 0) {
			for (BscDicCodeItem form : list) {
				BasicCombo combo = new BasicCombo();
				combo.setLabel(form.getItemName());
				combo.setValue(form.getItemCode());
				combo.setId(form.getItemId());
				result.add(combo);
			}
		}
		return result;
	}

	/**
	 * 将数据字典类型数据转换成下拉框数据
	 * 
	 * @param list
	 *            数据字典类型数据集合
	 * @param allowBlank
	 *            下拉框是否允许有空白项
	 * @return 下拉框json数据
	 */
	public static List<BasicCombo> convertCodeTypeToBasicCombo(
			List<BscDicCodeType> list, boolean allowBlank) {
		List<BasicCombo> result = new ArrayList<BasicCombo>();

		if (allowBlank) {
			result.add(BasicCombo.getBlankItem());
		}

		if (list != null && list.size() > 0) {
			for (BscDicCodeType form : list) {
				BasicCombo combo = new BasicCombo();
				combo.setLabel(form.getTypeCode());
				combo.setValue(form.getTypeCode());
				combo.setId(form.getTypeId());
				result.add(combo);
			}
		}
		return result;
	}

	/**
	 * 根据原始的数据字典子项集合组装成一个下拉框树
	 * @param rawList 原始的数据字典子项集合
	 * @return 树形下拉框
	 */
	public static List<BasicCombo> buildTreeCombo(List<BscDicCodeItem> rawList, boolean allowBlank) {
		if (rawList != null && rawList.size() > 0) {
			List<BscDicCodeItem> topList = getTopItems(rawList);
			if(topList != null && topList.size()>0){
				List<BasicCombo> result = convertCodeItemToBasicCombo(topList,allowBlank);
				loadChilren(result,rawList,allowBlank);
				return result;
			}
		}
		return null;
	}

	/**
	 * 递归为下拉框对象装载子下拉框数据
	 * @param comboList 下拉框对象集合
	 * @param rawList 原始的数据字典子项集合
	 */
	public static void loadChilren(List<BasicCombo> comboList,List<BscDicCodeItem> rawList, boolean allowBlank){
		if(comboList != null && comboList.size()>0 && rawList != null && rawList.size() > 0){
			for(BasicCombo combo:comboList){
				List<BscDicCodeItem> subList = getSubItemsByParentId(combo.getId(),rawList);
				if(subList != null && subList.size()>0){
					List<BasicCombo> children = convertCodeItemToBasicCombo(subList,allowBlank);
					if(children != null && children.size()>0){
						combo.setChildren(children);
						loadChilren(children,rawList,allowBlank);
					}
				}
			}
		}
	}
	/**
	 * 根据指定数据字典子项父主键获取指定数据字典子项集合中的直接关联子子项
	 * @param parentItemId 数据字典子项父主键
	 * @param rawList 指定数据字典子项集合
	 * @return 数据字典子子项集合
	 */
	public static List<BscDicCodeItem> getSubItemsByParentId(String parentItemId,
			List<BscDicCodeItem> rawList) {
		 List<BscDicCodeItem> result = null;
		if (parentItemId != null && parentItemId.trim().length() > 0
				&& rawList != null && rawList.size() > 0) {
			result = new ArrayList<BscDicCodeItem>();
			for(BscDicCodeItem form : rawList){
				if(parentItemId.equals(form.getItemParentId())){
					result.add(form);
					rawList.remove(form);
				}
			}
		}
		return result;
	}
	
	/**
	 * 获取指定数据字典子项集合中的顶级子项
	 * @param rawList 指定数据字典子项集合
	 * @return 顶级数据字典子项集合
	 */
	public static List<BscDicCodeItem> getTopItems(List<BscDicCodeItem> rawList) {
		 List<BscDicCodeItem> result = null;
		if (rawList != null && rawList.size() > 0) {
			result = new ArrayList<BscDicCodeItem>();
			for(BscDicCodeItem form : rawList){
				if(form.getItemParentId() == null){
					result.add(form);
					rawList.remove(form);
				}
			}
		}
		return result;
	}
	
	/**
	 * 构造树
	 * @param itemParent
	 * @param itemList
	 */
	public static void buildTypeTree(BscDicCodeType typeRoot, List<BscDicCodeItem> itemList) {
		//树节点名称
		typeRoot.setName("<font color='#e38606'><b>"+typeRoot.getTypeName()+"</b></font>");
		typeRoot.setOpen(true);
		typeRoot.setIconSkin("tree");
		typeRoot.setFont("{'color':'#e38606', 'font-weight':'bold'}");
		
		//将item对象装载到type对象的children当中
        for (Iterator<BscDicCodeItem> it = itemList.iterator(); it.hasNext();) {  
        	BscDicCodeItem item = (BscDicCodeItem) it.next();  
        	
            if (null == item.getItemParentId() && null != item.getTypeCode() && item.getTypeCode().equals(typeRoot.getTypeId())) {
            	
                if (null == typeRoot.getChildren())  
                	typeRoot.setChildren(new ArrayList<BscDicCodeItem>());
                
                buildItemTreeNode(item);
                
                typeRoot.getChildren().add(item);  
                it.remove();  
            }  
        }
        
        //如果list都清空了，那就装载完成马上返回
        if (itemList.size() == 0)  
            return;
        
        //递归装载其他children的children
        if (null != typeRoot.getChildren()) {  
            for (BscDicCodeItem item : typeRoot.getChildren()) {  
            	buildItemTree(item, itemList);  
            }  
        }  
    }
	
	/**
	 * 构造树
	 * @param itemParent
	 * @param itemList
	 */
	private static void buildItemTree(BscDicCodeItem itemParent, List<BscDicCodeItem> itemList) {
		
		//先为parent节点装载children
        for (Iterator<BscDicCodeItem> it = itemList.iterator(); it.hasNext();) {  
        	BscDicCodeItem item = (BscDicCodeItem) it.next();  
        	
            if (null != item.getItemParentId() && item.getItemParentId().equals(itemParent.getItemId())) {
            	
                if (null == itemParent.getChildren())  
                    itemParent.setChildren(new ArrayList<BscDicCodeItem>());
                
                buildItemTreeNode(item);
                
                itemParent.getChildren().add(item);  
                it.remove();  
            }  
        }
        
        //如果list都清空了，那就装载完成马上返回
        if (itemList.size() == 0)  
            return;
        
        //递归装载其他children的children
        if (null != itemParent.getChildren()) {  
            for (BscDicCodeItem item : itemParent.getChildren()) {  
            	buildItemTree(item, itemList);  
            }  
        }  
    }
	
	private static void buildItemTreeNode(BscDicCodeItem item){
		String htmlName = item.getItemName() != null ? item.getItemName() : "";
		
		if(Status.isOne(item.getItemIsActive()))
			htmlName += "&nbsp;<span class='icon-16-ok' title='已启用'></span>";
		else
			htmlName += "&nbsp;<span class='icon-16-delete' title='已禁用'></span>";
		
		if(Status.isOne(item.getItemIsLock()))
			htmlName += "<span class='icon-16-lock' title='已锁定'></span>";
		else
			htmlName += "<span class='icon-16-unlock' title='未锁定'></span>";
		
		item.setName(htmlName);
        item.setOpen(true);
	}
	
	/**
	 * 构造树
	 * @param itemParent
	 * @param itemList
	 */
	public static void buildActiveTypeTree(BscDicCodeType typeRoot, List<BscDicCodeItem> itemList) {
		//树节点名称
		typeRoot.setName(typeRoot.getTypeName());
		typeRoot.setOpen(true);
		typeRoot.setIconSkin("tree");
		
		//将item对象装载到type对象的children当中
        for (Iterator<BscDicCodeItem> it = itemList.iterator(); it.hasNext();) {  
        	BscDicCodeItem item = (BscDicCodeItem) it.next();  
        	
            if (null == item.getItemParentId() && null != item.getTypeCode() && item.getTypeCode().equals(typeRoot.getTypeId())) {
            	
                if (null == typeRoot.getChildren())  
                	typeRoot.setChildren(new ArrayList<BscDicCodeItem>());
                
                buildActiveItemTreeNode(item);
                
                typeRoot.getChildren().add(item);  
                it.remove();  
            }  
        }
        
        //如果list都清空了，那就装载完成马上返回
        if (itemList.size() == 0)  
            return;
        
        //递归装载其他children的children
        if (null != typeRoot.getChildren()) {  
            for (BscDicCodeItem item : typeRoot.getChildren()) {  
            	buildActiveItemTree(item, itemList);  
            }  
        }  
    }
	
	private static void buildActiveItemTreeNode(BscDicCodeItem item){
		item.setName(item.getItemName() != null ? item.getItemName() : "");
        item.setOpen(true);
	}
	
	/**
	 * 构造树
	 * @param itemParent
	 * @param itemList
	 */
	private static void buildActiveItemTree(BscDicCodeItem itemParent, List<BscDicCodeItem> itemList) {
		
		//先为parent节点装载children
        for (Iterator<BscDicCodeItem> it = itemList.iterator(); it.hasNext();) {  
        	BscDicCodeItem item = (BscDicCodeItem) it.next();  
        	
            if (null != item.getItemParentId() && item.getItemParentId().equals(itemParent.getItemId())) {
            	
                if (null == itemParent.getChildren())  
                    itemParent.setChildren(new ArrayList<BscDicCodeItem>());
                
                buildActiveItemTreeNode(item);
                
                itemParent.getChildren().add(item);  
                it.remove();  
            }  
        }
        
        //如果list都清空了，那就装载完成马上返回
        if (itemList.size() == 0)  
            return;
        
        //递归装载其他children的children
        if (null != itemParent.getChildren()) {  
            for (BscDicCodeItem item : itemParent.getChildren()) {  
            	buildActiveItemTree(item, itemList);  
            }  
        }  
    }
}
